package com.steakliu.gateway.dao;

import com.steakliu.gateway.definition.GateWayPredicateDefinition;
import com.steakliu.gateway.definition.GatewayRouteDefinition;
import com.steakliu.gateway.dto.RouteDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class RouteDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save(GatewayRouteDefinition definition){
        String sql = "INSERT INTO route (route_id, route_name, url, create_time, create_name, update_time, update_name, remark, del_flag, `order`, predicates, filters) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
        int i = jdbcTemplate.update(sql, definition.getRouteId(), definition.getRouteName(), definition.getUri(), new Date(), definition.getCreateName()
                , null, null, definition.getRemark(), 0, definition.getOrder(), definition.getPredicates().toString(), definition.getFilters().toString()
        );
    }

    public List<RouteDto> list(){
        String sql = "select * from route";
        List<RouteDto> query = jdbcTemplate.query(sql, new Object[]{}, new BeanPropertyRowMapper<>(RouteDto.class));
        return query;
    }
}
